package com.taotao.common.ApiService;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
/**
 * 使用HttpClient技术调用外部接口
 * @author Administrator
 *
 */
public class ApiService {
	
	private CloseableHttpClient httpclient;
	
	public ApiService(CloseableHttpClient httpclient) {
		super();
		this.httpclient = httpclient;
	}

	/**
	 * HttpClient 无参的get请求
	 * 
	 * @param url
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public HttpClientResult doGet(String url) throws ClientProtocolException,
			IOException {
		// 创建http GET请求
		HttpGet httpGet = new HttpGet(url);
		// 调用接口后响应对象 : 响应状态 响应内容
		CloseableHttpResponse response = null;
		try {
			// 执行请求
			response = httpclient.execute(httpGet);
			// 获取响应状态码
			int statusCode = response.getStatusLine().getStatusCode();
			// 获取响应数据
			String content = EntityUtils
					.toString(response.getEntity(), "UTF-8");
			// 封装状态码和数据返回
			return new HttpClientResult(statusCode, content);
		} finally {
			if (response != null) {
				response.close();
			}
		}
	}

	/**
	 * HttpClient 有参的get请求
	 * 
	 * @author Administrator
	 * @throws URISyntaxException
	 * @throws IOException
	 * @throws ClientProtocolException
	 *
	 */
	public HttpClientResult doGetParam(String url, Map<String, String> params)
			throws URISyntaxException, ClientProtocolException, IOException {
		// 定义请求的参数
		URIBuilder uriBuilder = new URIBuilder(url);
		// 参数传递方式
		// Map<String,String> params = null;
		if (params != null && !params.isEmpty()) {
			Set<String> keySet = params.keySet();
			for (String key : keySet) {
				uriBuilder.setParameter(key, params.get(key));
			}
		}
		// build方法创建出URI对象
		URI uri = uriBuilder.build();
		// 创建http GET请求
		HttpGet httpGet = new HttpGet(uri);
		CloseableHttpResponse response = null;
		try {
			// 执行请求
			response = httpclient.execute(httpGet);
			int statusCode = response.getStatusLine().getStatusCode();
			String content = EntityUtils
					.toString(response.getEntity(), "UTF-8");
			return new HttpClientResult(statusCode, content);
		} finally {
			if (response != null) {
				response.close();
			}
		}
	}

	/**
	 * HttpClient 无参的post请求
	 * 
	 * @param url
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public HttpClientResult doPost(String url) throws ClientProtocolException,
			IOException {
		// 创建http POST请求 输入URL
		HttpPost httpPost = new HttpPost(url);
		// 模拟了浏览器来进行访问
		httpPost.setHeader(
				"User-Agent",
				"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36");
		CloseableHttpResponse response = null;
		try {
			// 执行请求
			response = httpclient.execute(httpPost);
			// 获取响应结果
			int statusCode = response.getStatusLine().getStatusCode();
			// 获取响应数据
			String content = EntityUtils
					.toString(response.getEntity(), "UTF-8");
			return new HttpClientResult(statusCode, content);
		} finally {
			if (response != null) {
				response.close();
			}
		}
	}

	public HttpClientResult doPostParam(String url, Map<String, String> params)
			throws ClientProtocolException, IOException {
		// 创建http POST请求
		HttpPost httpPost = new HttpPost(url);

		// 设置参数
		List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);
		Set<String> keySet = params.keySet();
		for (String key : keySet) {
			parameters.add(new BasicNameValuePair(key, params.get(key)));
		}
		// 构造一个form表单式的实体
		UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters);
		// 将请求实体设置到httpPost对象中 设置post请求提交的表单
		httpPost.setEntity(formEntity);

		CloseableHttpResponse response = null;
		try {
			// 执行请求
			response = httpclient.execute(httpPost);
			// 获取响应状态码
			int statusCode = response.getStatusLine().getStatusCode();
			// 获取响应数据
			String content = EntityUtils
					.toString(response.getEntity(), "UTF-8");
			// 封装状态码和数据并返回
			return new HttpClientResult(statusCode, content);
		} finally {
			if (response != null) {
				response.close();
			}
		}
	}

}
